local XBarFriendButtons={};
local XBarFriendGroups={};
local start=1;
local max=9;
 
function XBarFriend_OnEvent(this,event)
-- ChatFrame1:AddMessage("XBar-Friend : "..event);
	if (event=="LOADING_END") then
		this:RegisterEvent("ADD_FRIEND");				
		this:RegisterEvent("RESET_FRIEND");
		this:RegisterEvent("DEL_FRIEND");
		this:RegisterEvent("ONLINE_FRIEND");
		this:RegisterEvent("VIEW_FRIEND_ALERT");
		this:RegisterEvent("SHOW_FRIEND_DETAIL");	
		this:RegisterEvent("PLAYER_MODEL_INFO_READY");
	end
-- Output
	local usrtxt={[1]=XBar["FriendV1"],[2]=XBar["FriendV2"]};
	local output="";
	for i=1,2 do
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[ONLINE%]",tostring(XBarFriend_Info("OnlineCount")));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[COUNT%]",tostring(XBarFriend_Info("FriendCount")));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[GROUPS%]",tostring(XBarFriend_GroupList("COUNT")));
	end
	if (XBar["FriendT1"]==true) then output=usrtxt[1]; end
	if (XBar["FriendT2"]==true) then
		if (XBar["FriendT1"]==true) then output=output.."\n"..usrtxt[2]; else output=usrtxt[2]; end
	end
	XBarFriend_F_Text:SetText(output);
-- EoL
end

function XBarFriend_Info(v1,v2,v3,v4)
	if (GetFriendCount("Friend")==0) then return "nil"; end
	local OnlineCount=0;
	local it=GetGuildInfo();
	if (v1=="FriendCount") then return GetFriendCount("Friend"); end
	if (v1=="OnlineCount") then
		for i=1,GetFriendCount("Friend") do
			local _,_,On,_,_,_,_,_,_,_=GetFriendInfo("Friend",i);
			if (On) then if (On==true) then OnlineCount=OnlineCount+1; end; end
		end
		return OnlineCount;
	end
end

function XBarFriend_GroupList(v1,v2)
	XBarFriendGroups={};
	local count=GetSocalGroupCount("Friend");
	if (not count or count==0) then count=0;
	else
		for i=1,count do
			local ID,Name,Sort=GetSocalGroupInfo("Friend",i);
			XBarFriendGroups[ID]={["Name"]=Name,["Sort"]=Sort};
		end
	end
	if (v1=="COUNT") then return count; end
	if (v1=="NAME" and count) then return XBarFriendGroups[v2]["Name"]; end
end

function XBarFriend_OnClick(key,wheel)
	local scroll=(wheel and 1) or false;
	if (key=="LBUTTON") then
		if (GetFriendCount("Friend")==0) then return "nil"; end
		XBarFriendButtons={};
		local count=0;
		for i=1,GetFriendCount("Friend") do
			local Name,GroupID,Online,Friends,Static,Top,DiedOf,Kills,RelType,RelLvl=GetFriendInfo("Friend",i);
			if (Online) then
				if (Online==true) then
					count=count+1;
					XBarFriendButtons[count]={
						icon=XBarFriend_Icon(Friends),
						CharName=Name,
						GetText=function() return Name; end,
						GetTooltip=function()
							 info="";
							 info=info.."|cffC5B358"..XLng["Ttip"]["Friend4"].."|r "..XBarFriend_GroupList("NAME",GroupID).."\n";
							 info=info.."|cff857318"..XLng["Ttip"]["Friend5"].."|r "..tostring(DiedOf).."\n";
							 info=info.."|cff857318"..XLng["Ttip"]["Friend6"].."|r"..tostring(Kills).."\n";
							 info=info.."\n";
							 info=info.."|cff00B2E5"..XLng["Ttip"]["LMOUSE_WSP"].."|r\n";
							 info=info.."|cff00B2E5"..XLng["Ttip"]["RMOUSE_INV"].."|r\n";
							 info=info.."|cff00B2E5"..XLng["Ttip"]["SCROLL_ON"].."|r\n";
							 return info; end,
						OnClick=function(this,key) XBarFriend_OnSelect(this,key); end,
						OnScroll=function(delta) XBarFriend_Scroll(delta); end,
					};
				end
			end
		end
		if (start>1) then for i=1,start-1 do table.remove(XBarFriendButtons,1); end; end
		while #XBarFriendButtons>max do table.remove(XBarFriendButtons); end
		XBar_PopupMenu.Buttons=XBarFriendButtons;
		XBar_PopupMenu_Toggle("XBarFriend",0,32,scroll);
	end
end

function XBarFriend_Scroll(delta)
	if (delta>0) then if (start>1) then start=start-1; end; end
	if (delta<0) then if (start<XBarFriend_Info("OnlineCount")-(max-1)) then start=start+1; end; end
	XBarFriend_OnClick("LBUTTON",true);
end

function XBarFriend_OnSelect(this,key)
	if (key=="LBUTTON") then ChatFrame_SendTell(XBar_PopupMenu.Buttons[this:GetID()].CharName); end
	if (key=="RBUTTON") then InviteByName(XBar_PopupMenu.Buttons[this:GetID()].CharName); end
end

function XBarFriend_Icon(both)
	if (both==true) then return "Interface\\Icons\\Pet_Froster01"; end
	if (both==false) then return "Interface\\Icons\\Pet_Froster02"; end
	return "Interface\\Icons\\Item_Mea_011";
end
